//
//  IndustryNewsViewController.m
//  Rail
//
//  Created by hychen on 2018/3/2.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "IndustryNewsViewController.h"
#import "NewsTableViewCell.h"
#import "HomeCycleModel.h"

@interface IndustryNewsViewController ()

@end

@implementation IndustryNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:self.model.dir_name];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"NewsTableViewCell"];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData
{
    self.isOpenFooter = NO;
    self.currentPage = 1;
    BMWeakSelf
    [HttpRequest loadNewsListDataWithPageNo:[NSString stringWithFormat:@"%ld",self.currentPage] dirId:self.model.dirid?:@"" success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_footer resetNoMoreData];
            if (object.count > 9) {
                weakSelf.isOpenFooter = YES;
            }
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData
{
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadNewsListDataWithPageNo:[NSString stringWithFormat:@"%ld",self.currentPage] dirId:self.model.dirid?:@"" success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewModel *model = [self.dataArray objectAtIndex:indexPath.row];
    NewsTableViewCell *cell = [NewsTableViewCell cellWithTableView:tableView class:[NewsTableViewCell class]];
    //赋值
    cell.theNewModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NewsModel *obj = [self.dataArray objectAtIndexSafe:indexPath.row];
    [JumpTool gotoIndustyNewsDetail:obj];
}
@end
